package 第十周.homework;

/*编写一个新选择排序法，该程序操作一个以整形数作为节点的列表*/
public class NewSorting {
    private NewSortingNode list;

    public NewSorting() {
        list = null;
    }
    public void add(int num)
    {
        NewSortingNode node  = new NewSortingNode(num);
        NewSortingNode current;

        if(list == null)
            list = node;
        else
        {
            current = list;
            while(current.next!=null)
                current = current.next;
            current.next = node;
        }
    }
    public void selection()
    {
        NewSortingNode min;
        int temp;
        NewSortingNode index = list;
        while(index != null) {
            min = index;
            NewSortingNode current = min.next;//创造动态链，将list与min与current连在一起。
            while (current != null) {
                if (current.number < min.number)//min节点所指在current所指前面。
                    min = current;
                current = current.next;
            }
            temp = min.number;
            min.number = index.number;
            index.number = temp;
            index = index.next;
        }
    }
    public String toString()
    {
        String result = "";
        NewSortingNode current = list;

        while(current!=null)
        {
            result += current.number+"\n";
            current =current.next;
        }
        return result;
    }
    private class NewSortingNode {
        public int number;
        public NewSortingNode next;

        public NewSortingNode(int num) {
            number = num;
            next = null;
        }
    }
}


